package com.capgemini.teamtwo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.capgemini.teamtwo.entity.ProjectSchedule;
import com.capgemini.teamtwo.mapper.ProjectScheduleMapper;
import com.capgemini.teamtwo.service.ProjectScheduleService;
import com.github.abel533.echarts.Option;
import com.github.abel533.echarts.axis.CategoryAxis;
import com.github.abel533.echarts.axis.ValueAxis;
import com.github.abel533.echarts.code.Tool;
import com.github.abel533.echarts.json.GsonOption;
import com.github.abel533.echarts.series.Line;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * ClassName:BiaoController <br/>
 * Description:todo <br/>
 * Date:2022/8/27 13:14 <br/>
 *
 * @author 于鸿飞
 */
@Api("返回表单")
@RestController
@RequestMapping("/line")
public class LineController {
    @Resource
    ProjectScheduleService projectScheduleService;
    @Resource
    ProjectScheduleMapper projectScheduleMapper;
        /**
         * 折线图的 测试用例
         * @return
         */
        @ApiOperation("根据id获取项目进度信息")
        @RequestMapping(value = "/line/{projectid}",method =RequestMethod.POST,
                produces = {"application/text;charset=UTF-8"})
        @ResponseBody()
        public String line(@PathVariable("projectid")Integer projectid) throws IOException {
            LambdaQueryWrapper<ProjectSchedule> lambdaQueryWrapper=new QueryWrapper<ProjectSchedule>().lambda()
                    .select(ProjectSchedule::getProjectDate)
                    .eq(ProjectSchedule::getProjectId,projectid)
                    .orderByAsc(ProjectSchedule::getProjectDate);
            List<ProjectSchedule> list1 = projectScheduleMapper.selectList(lambdaQueryWrapper.eq(ProjectSchedule::getProjectId, projectid));
            LambdaQueryWrapper<ProjectSchedule> lambdaQueryWrapper1=new QueryWrapper<ProjectSchedule>().lambda()
                    .select(ProjectSchedule::getPlan)
                    .eq(ProjectSchedule::getProjectId,projectid)
                    .orderByAsc(ProjectSchedule::getProjectDate);

            List<ProjectSchedule> list2 = projectScheduleMapper.selectList(lambdaQueryWrapper1.eq(ProjectSchedule::getProjectId, projectid));
            LambdaQueryWrapper<ProjectSchedule> lambdaQueryWrapper2=new QueryWrapper<ProjectSchedule>().lambda()
                    .select(ProjectSchedule::getActual)
                    .eq(ProjectSchedule::getProjectId,projectid)
                    .orderByAsc(ProjectSchedule::getProjectDate);
            List<ProjectSchedule> list3 = projectScheduleMapper.selectList(lambdaQueryWrapper2.eq(ProjectSchedule::getProjectId, projectid));
            String title = "项目进度";
            String[] types = {"预计完成任务","实际完成任务"};
            Object[] objects = list1.stream().map(ProjectSchedule::getProjectDate).toArray();
            int[] int2 = list2.stream().mapToInt(e -> e.getPlan() == null ? 0 : e.getPlan()).toArray();
            int[] int3 = list3.stream().mapToInt(e -> e.getActual() == null ? 0 : e.getActual()).toArray();
            int[][] datas={int2,int3};
            Object[] weeks=objects;
            Option option = new GsonOption();

            option.title().text(title).x("left");
            option.legend().data(types).x("center");

            option.tooltip().show(true);
            option.toolbox().show(true).feature(Tool.magicType,Tool.dataZoom);

            CategoryAxis categoryAxis = new CategoryAxis();

            categoryAxis.data(weeks);
            option.xAxis(categoryAxis);
            option.yAxis(new ValueAxis());


            //循环为了获取多条折线数据
            for(int i =0;i< types.length;i++){

                //创建line对象
                Line line = new Line();
                String type = types[i];
                line.name(type);
                for(int j = 0; j<weeks.length;j++){

                    line.data(datas[i][j]);

                }
                option.series(line);
            }
            return option.toString();
        }
    }

